define(['jquery', 'jquery.validate'],function($){
    $.validator.addMethod("check_email", function(value, element) {
        var reg = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i;
        return this.optional(element) || (reg.test(value));
    }, '邮箱地址错误');
    $.validator.addMethod("check_password", function(value, element) {
        //var reg = /\s|^[\d]{8,16}$|^[a-zA-Z]{8,16}$/;
        var reg = /^(?![a-z]+$)(?![A-Z]+$)(?![0-9]+$)[a-zA-Z0-9]{8,16}$/;
        return this.optional(element) || (reg.test(value));
    }, '由8-16位字母、数字组成,区分大小写且不允许有空格,不能是纯字母或数字');
    $.validator.addMethod("check_mobile", function(value, element) {
        var reg = /^(13[0-9]{9})|(15[0-9]{9})|(18[0-9]{9})$/;
        return this.optional(element) || reg.test(value);
    }, '手机号码格式错误');
    $.validator.addMethod("mobile_phone", function(value, element) {
        var reg = /^1[0-9]{10}$/;
        return this.optional(element) || (reg.test(value));
    }, '手机号码格式错误');
    $.validator.addMethod("custom_remote", function(value, element,param) {

        if ( this.optional( element ) ) {
            return "dependency-mismatch";
        }

        var previous = this.previousValue( element ),
            validator, data,successHandler,errorHandler,parseMessage,beforeSendHandler;

        if (!this.settings.messages[ element.name ] ) {
            this.settings.messages[ element.name ] = {};
        }
        previous.originalMessage = this.settings.messages[ element.name ].remote;
        this.settings.messages[ element.name ].custom_remote = previous.message;
        //console.log(arguments);
        if(typeof(param)=="object")
        {
            if(param.successHandler)
            {
                successHandler = param.successHandler;
            }
            if(param.errorHandler)
            {
                errorHandler = param.errorHandler;
            }
            if(param.parseMessage)
            {
                parseMessage = param.parseMessage;
            }
            if(param.beforeSendHandler){
                beforeSendHandler = param.beforeSendHandler;
            }
            //console.log(param.successHandler);
        }

        param = typeof param === "string" && { url: param } || param;

        if ( previous.old === value ) {
            return previous.valid;
        }

        previous.old = value;
        validator = this;
        this.startRequest( element );
        data = {};
        data[ element.name ] = value;
        if(beforeSendHandler && typeof(beforeSendHandler)=="function" && beforeSendHandler.call(this,value)===false){
            validator.prepareElement( element );
            validator.successList.push( element );
            delete validator.invalid[ element.name ];
            validator.showErrors();
            previous.valid = true;
            validator.stopRequest( element, true );
            return true;
        }
        $.ajax( $.extend( true, {
            url: param,
            method:"post",
            mode: "abort",
            port: "validate" + element.name,
            dataType: "json",
            data: data,
            context: validator.currentForm,
            success: function( response ) {
                //console.log(response);
                var valid = response.success === true,
                    errors, message, submitted;
                validator.settings.messages[ element.name ].custom_remote = response.message || previous.originalMessage;
                if ( valid ) {
                    if($.isFunction(successHandler))
                    {
                        successHandler(response);
                    }
                    submitted = validator.formSubmitted;
                    validator.prepareElement( element );
                    validator.formSubmitted = submitted;
                    validator.successList.push( element );
                    delete validator.invalid[ element.name ];
                    validator.showErrors();
                } else {
                    if($.isFunction(errorHandler))
                    {
                        errorHandler(response);
                    }
                    errors = {};
                    var msg = $.isFunction(parseMessage)?parseMessage(response):response.message;
                    message = msg || validator.defaultMessage( element, "remote" );
                    errors[ element.name ] = previous.message = $.isFunction( message ) ? message( value ) : message;
                    validator.invalid[ element.name ] = true;
                    validator.showErrors( errors );
                }
                previous.valid = valid;
                validator.stopRequest( element, valid );
            }
        }, param ) );
        return "pending";
    }, '手机号码格式错误');

    $.validator.addMethod("require_from_group", function(value, element, options) {
        var numberRequired = options[0];
        var selector = options[1];
        //console.log(selector);
        //Look for our selector within the parent form
        var validOrNot = $(selector, element.form).filter(function() {
                // Each field is kept if it has a value
                return $(this).val();
                // Set to true if there are enough, else to false
            }).length >= numberRequired;
        // The elegent part - this element needs to check the others that match the
        // selector, but we don't want to set off a feedback loop where each element
        // has to check each other element. It would be like:
        // Element 1: "I might be valid if you're valid. Are you?"
        // Element 2: "Let's see. I might be valid if YOU'RE valid. Are you?"
        // Element 1: "Let's see. I might be valid if YOU'RE valid. Are you?"
        // ...etc, until we get a "too much recursion" error.
        //
        // So instead we
        // 1) Flag all matching elements as 'currently being validated'
        // using jQuery's .data()
        // 2) Re-run validation on each of them. Since the others are now
        //  flagged as being in the process, they will skip this section,
        //  and therefore won't turn around and validate everything else
        // 3) Once that's done, we remove the 'currently being validated' flag
        //  from all the elements
        if(!$(element).data('being_validated')) {
            var fields = $(selector, element.form);
            fields.data('being_validated', true);
            // .valid() means "validate using all applicable rules" (which
            // includes this one)
            fields.valid();
            fields.data('being_validated', false);
        }
        return validOrNot;
        // {0} below is the 0th item in the options field
    }, $.validator.format("Please fill out at least {0} of these fields."));
    $.validator.addMethod("depend", function(value, element,depend) {
        return this.optional(element) || $(depend).valid();
    }, '验证错误');
    $.validator.addMethod("regex", function(value, element,regex) {
        return this.optional(element) || regex.test(value);
    }, '验证错误');
    return $;
});